using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using MEA.AE_Data;
using MEA.AE_Data.Config;

namespace MEA.AE_BusinessLogic
{
	public class UIEvents : Form
	{
		public enum EEventColors
		{
			eRedRowColor,
			eRedRowErrColor,
			eGreenRowColor,
			eGreenRowErrColor,
			eLastColor
		}

		public struct DataGridEventsInfoData
		{
			public EventLogEntry myEvent;

			public string[] row;

			public EEventColors colorNr;

			public bool showRow;
		}

		private UIEventFilter uiEventsFilter;

		private EventFilterData eventFilterData;

		private List<DataGridEventsInfoData> dataGridEventsInfoData;

		public FileInfoHnd fileInfoEv;

		private Xml_Log.CXmlFileLogInfo dataLogInfo;

		private Color[] infoColor = new Color[4]
		{
			Color.FromArgb(229, 184, 183),
			Color.FromArgb(255, 154, 153),
			Color.FromArgb(204, 255, 204),
			Color.FromArgb(100, 255, 100)
		};

		public int myListNr;

		private bool startUIEventFilterDataInit = true;

		private IContainer components;

		public DataGridView dataGridView1;

		private DataGridViewTextBoxColumn Timestamp;

		private DataGridViewTextBoxColumn DTC;

		private DataGridViewTextBoxColumn Status;

		private DataGridViewTextBoxColumn entrycount;

		private DataGridViewTextBoxColumn Description;

		private ToolStrip toolStripEv;

		private ToolStripButton toolStripButtonFileInfo;

		private ToolStripButton toolStripButtonEvShowFilter;

		private ToolStripLabel toolStripLabel1;

		private ToolStripSeparator toolStripSeparator1;

		private Label notvalidtimestampLabel;

		public UIEvents(int mynr)
		{
			myListNr = mynr;
			InitializeComponent();
			CenterToParent();
			SetEventsFilterDataEmpty();
			Focus();
		}

		private bool IsEvent2Showig(ref DataGridEventsInfoData info)
		{
			bool result = false;
			bool flag = true;
			if (showTimeInvalid(info.myEvent.timeStampInvalid) && ((info.myEvent.ecu == ConfigData.e_ECUID.A && eventFilterData.showECUAEvents) || (info.myEvent.ecu == ConfigData.e_ECUID.B && eventFilterData.showECUBEvents)))
			{
				for (int i = 0; i < eventFilterData.listOfShowedEvents.Count; i++)
				{
					if (!flag)
					{
						break;
					}
					if (eventFilterData.listOfShowedEventsID[i] == info.myEvent.eventID)
					{
						flag = false;
						result = eventFilterData.listOfShowedEvents[i];
					}
				}
			}
			return result;
		}

		private bool showTimeInvalid(bool tmInval)
		{
			bool result = false;
			if (eventFilterData.showEventsTimeInvalid)
			{
				result = true;
			}
			else if (!tmInval)
			{
				result = true;
			}
			return result;
		}

		private bool IsEventIDVisiblex(ref DataGridEventsInfoData info)
		{
			bool result = false;
			bool flag = true;
			switch (info.myEvent.ecu)
			{
			case ConfigData.e_ECUID.A:
			{
				if (!eventFilterData.showECUAEvents)
				{
					break;
				}
				for (int j = 0; j < eventFilterData.listOfShowedEvents.Count; j++)
				{
					if (!flag)
					{
						break;
					}
					if (eventFilterData.listOfShowedEventsID[j] == info.myEvent.eventID)
					{
						flag = false;
						result = eventFilterData.listOfShowedEvents[j];
					}
				}
				break;
			}
			case ConfigData.e_ECUID.B:
			{
				if (!eventFilterData.showECUBEvents)
				{
					break;
				}
				for (int i = 0; i < eventFilterData.listOfShowedEvents.Count; i++)
				{
					if (!flag)
					{
						break;
					}
					if (eventFilterData.listOfShowedEventsID[i] == info.myEvent.eventID)
					{
						result = eventFilterData.listOfShowedEvents[i];
						flag = false;
					}
				}
				break;
			}
			}
			return result;
		}

		private void SetFilter(object sender, ItemCheckEventArgs e)
		{
			UIEventFilter uIEventFilter = (UIEventFilter)sender;
			dataGridView1.SuspendLayout();
			int cnt = 0;
			uiEventsFilter.EnableDisableFilterMenu(false);
			int i;
			DataGridEventsInfoData info;
			switch (uIEventFilter.fIlterType)
			{
			case UIEventFilter.ESetEvFilter.eSetA:
				eventFilterData.showECUAEvents = uIEventFilter.setVal;
				for (i = 0; i < dataGridEventsInfoData.Count; i++)
				{
					info = dataGridEventsInfoData[i];
					info.showRow = IsEvent2Showig(ref info);
					dataGridEventsInfoData[i] = info;
					if (info.showRow)
					{
						cnt++;
					}
				}
				break;
			case UIEventFilter.ESetEvFilter.eSetB:
				eventFilterData.showECUBEvents = uIEventFilter.setVal;
				for (i = 0; i < dataGridEventsInfoData.Count; i++)
				{
					info = dataGridEventsInfoData[i];
					info.showRow = IsEvent2Showig(ref info);
					dataGridEventsInfoData[i] = info;
					if (info.showRow)
					{
						cnt++;
					}
				}
				break;
			case UIEventFilter.ESetEvFilter.eSetVal:
			{
				int num = eventFilterData.listOfShowedEventsID[uIEventFilter.rowx];
				for (i = 0; i < dataGridEventsInfoData.Count; i++)
				{
					info = dataGridEventsInfoData[i];
					info.showRow = IsEvent2Showig(ref info);
					dataGridEventsInfoData[i] = info;
					if (info.showRow)
					{
						cnt++;
					}
				}
				break;
			}
			case UIEventFilter.ESetEvFilter.eSetTextBoxIDs:
				for (i = 0; i < dataGridEventsInfoData.Count; i++)
				{
					info = dataGridEventsInfoData[i];
					info.showRow = IsEvent2Showig(ref info);
					if (info.showRow)
					{
						cnt++;
					}
					dataGridEventsInfoData[i] = info;
				}
				break;
			case UIEventFilter.ESetEvFilter.eSetAll:
				if (eventFilterData.listOfShowedEvents[0])
				{
					for (i = 0; i < dataGridEventsInfoData.Count; i++)
					{
						info = dataGridEventsInfoData[i];
						if (IsEvent2Showig(ref info))
						{
							cnt++;
							info.showRow = true;
							dataGridEventsInfoData[i] = info;
						}
					}
					break;
				}
				cnt = 0;
				for (i = 0; i < dataGridEventsInfoData.Count; i++)
				{
					info = dataGridEventsInfoData[i];
					if (info.showRow)
					{
						info.showRow = false;
						dataGridEventsInfoData[i] = info;
					}
				}
				break;
			case UIEventFilter.ESetEvFilter.eSetShowEventsTimeInValid:
				eventFilterData.showEventsTimeInvalid = uIEventFilter.setVal;
				for (i = 0; i < dataGridEventsInfoData.Count; i++)
				{
					info = dataGridEventsInfoData[i];
					info.showRow = IsEvent2Showig(ref info);
					dataGridEventsInfoData[i] = info;
					if (info.showRow)
					{
						cnt++;
					}
				}
				break;
			}
			int rowCnt;
			Invoke((MethodInvoker)delegate
			{
				rowCnt = 0;
				dataGridView1.RowCount = cnt;
				for (i = 0; i < dataGridEventsInfoData.Count; i++)
				{
					info = dataGridEventsInfoData[i];
					if (info.showRow)
					{
						dataGridView1.Rows[rowCnt].SetValues(info.row);
						dataGridView1.Rows[rowCnt++].DefaultCellStyle.BackColor = infoColor[(int)info.colorNr];
					}
				}
				dataGridView1.ResumeLayout();
				uiEventsFilter.EnableDisableFilterMenu(true);
			});
		}

		public void UIEvents_FormClosing()
		{
			if (uiEventsFilter != null)
			{
				eventFilterData.listOfShowedEvents.Clear();
				eventFilterData.listOfShowedEventsID.Clear();
				eventFilterData.filterListOfEvents.Clear();
				uiEventsFilter.Close();
			}
			if (fileInfoEv != null)
			{
				fileInfoEv.Disp();
			}
			dataGridEventsInfoData.Clear();
		}

		private void SetEventsFilterDataEmpty()
		{
			eventFilterData = default(EventFilterData);
			eventFilterData.showECUAEvents = true;
			eventFilterData.showECUBEvents = true;
			eventFilterData.listOfShowedEvents = new List<bool>();
			eventFilterData.listOfShowedEventsID = new List<int>();
			eventFilterData.filterListOfEvents = new List<EventLogEntry>();
			dataGridEventsInfoData = new List<DataGridEventsInfoData>();
		}

		public void AddEventRows(List<EventLogEntry> events, ConfigData.e_ECUS ecu)
		{
			Invoke((MethodInvoker)delegate
			{
				List<DataGridEventsInfoData> list = new List<DataGridEventsInfoData>(events.Count + dataGridEventsInfoData.Count);
				list.AddRange(dataGridEventsInfoData);
				for (int i = 0; i < events.Count; i++)
				{
					DataGridEventsInfoData item = default(DataGridEventsInfoData);
					EventLogEntry myEvent = events[i];
					item.myEvent = myEvent;
					item.row = item.myEvent.createRow();
					if (item.myEvent.beginEvent)
					{
						if (item.myEvent.timeStampInvalid)
						{
							item.colorNr = EEventColors.eRedRowErrColor;
							item.showRow = false;
						}
						else
						{
							item.colorNr = EEventColors.eRedRowColor;
							item.showRow = true;
						}
					}
					else if (item.myEvent.timeStampInvalid)
					{
						item.colorNr = EEventColors.eGreenRowErrColor;
						item.showRow = false;
					}
					else
					{
						item.colorNr = EEventColors.eGreenRowColor;
						item.showRow = true;
					}
					list.Add(item);
				}
				dataGridEventsInfoData.Clear();
				dataGridEventsInfoData.Capacity = list.Count;
				if (ecu == ConfigData.e_ECUS.ECU_B)
				{
					IOrderedEnumerable<DataGridEventsInfoData> orderedEnumerable = list.OrderByDescending((DataGridEventsInfoData element) => Convert.ToDateTime(element.myEvent.timestamp));
					foreach (DataGridEventsInfoData item2 in orderedEnumerable)
					{
						dataGridEventsInfoData.Add(item2);
					}
				}
				else
				{
					dataGridEventsInfoData = list;
				}
				int num = 0;
				for (int num2 = 0; num2 < dataGridEventsInfoData.Count; num2++)
				{
					if (dataGridEventsInfoData[num2].showRow)
					{
						num++;
					}
				}
				dataGridView1.RowCount = num;
				num = 0;
				for (int num3 = 0; num3 < dataGridEventsInfoData.Count; num3++)
				{
					DataGridEventsInfoData item = dataGridEventsInfoData[num3];
					if (item.showRow)
					{
						dataGridView1.Rows[num].SetValues(item.row);
						dataGridView1.Rows[num++].DefaultCellStyle.BackColor = infoColor[(int)item.colorNr];
					}
				}
				events.Clear();
			});
		}

		private void b_SetFilter_Click(object sender, EventArgs e)
		{
			if (uiEventsFilter == null || !uiEventsFilter.eventFilterShowed)
			{
				uiEventsFilter = new UIEventFilter(ref this.eventFilterData);
				if (startUIEventFilterDataInit)
				{
					EventFilterData eventFilterData = new EventFilterData
					{
						filterListOfEvents = new List<EventLogEntry>(),
						listOfShowedEvents = new List<bool>(),
						listOfShowedEventsID = new List<int>()
					};
					foreach (DataGridEventsInfoData dataGridEventsInfoDatum in dataGridEventsInfoData)
					{
						if (!eventFilterData.listOfShowedEventsID.Contains(dataGridEventsInfoDatum.myEvent.eventID))
						{
							eventFilterData.listOfShowedEventsID.Add(dataGridEventsInfoDatum.myEvent.eventID);
							eventFilterData.filterListOfEvents.Add(dataGridEventsInfoDatum.myEvent);
						}
					}
					IOrderedEnumerable<EventLogEntry> orderedEnumerable = eventFilterData.filterListOfEvents.OrderBy((EventLogEntry element) => element.eventID);
					foreach (EventLogEntry item in orderedEnumerable)
					{
						this.eventFilterData.filterListOfEvents.Add(item);
						this.eventFilterData.listOfShowedEvents.Add(true);
						this.eventFilterData.listOfShowedEventsID.Add(item.eventID);
					}
					this.eventFilterData.showECUAEvents = true;
					this.eventFilterData.showECUBEvents = true;
					startUIEventFilterDataInit = false;
				}
				uiEventsFilter.setUIEventFilter(ref this.eventFilterData);
				uiEventsFilter.Show();
				uiEventsFilter.ev_EVFilterSet += SetFilter;
			}
			uiEventsFilter.Focus();
		}

		public void addInfoSrm(Xml_Log.CXmlFileLogInfo infostr)
		{
			dataLogInfo = infostr;
		}

		private void b_EventWinResizeEnd(object sender, EventArgs e)
		{
			int num = base.Width;
			int num2 = base.Height;
			dataGridView1.Width = num - 34 - 12;
			dataGridView1.Height = num2 - 68 - 22;
			Point location = notvalidtimestampLabel.Location;
			location.Y = dataGridView1.Height + 4 + dataGridView1.Location.Y;
			notvalidtimestampLabel.Location = location;
		}

		private void b_FileInfoClick(object sender, EventArgs e)
		{
			if (fileInfoEv == null || !fileInfoEv.fileinfoShowed)
			{
				fileInfoEv = new FileInfoHnd();
				fileInfoEv.SetFileInfoStrm(ref dataLogInfo, base.Location);
				fileInfoEv.ShowFileInfo();
				fileInfoEv.SetShow();
			}
			fileInfoEv.SetFocus();
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing && components != null)
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		private void InitializeComponent()
		{
			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MEA.AE_BusinessLogic.UIEvents));
			this.dataGridView1 = new System.Windows.Forms.DataGridView();
			this.Timestamp = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.DTC = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.Status = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.entrycount = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.Description = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.toolStripEv = new System.Windows.Forms.ToolStrip();
			this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
			this.toolStripButtonFileInfo = new System.Windows.Forms.ToolStripButton();
			this.toolStripButtonEvShowFilter = new System.Windows.Forms.ToolStripButton();
			this.toolStripLabel1 = new System.Windows.Forms.ToolStripLabel();
			this.notvalidtimestampLabel = new System.Windows.Forms.Label();
			((System.ComponentModel.ISupportInitialize)this.dataGridView1).BeginInit();
			this.toolStripEv.SuspendLayout();
			base.SuspendLayout();
			this.dataGridView1.AllowUserToAddRows = false;
			this.dataGridView1.AllowUserToDeleteRows = false;
			this.dataGridView1.AllowUserToResizeRows = false;
			this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.dataGridView1.Columns.AddRange(this.Timestamp, this.DTC, this.Status, this.entrycount, this.Description);
			this.dataGridView1.Location = new System.Drawing.Point(17, 31);
			this.dataGridView1.Name = "dataGridView1";
			this.dataGridView1.RowHeadersVisible = false;
			this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
			this.dataGridView1.Size = new System.Drawing.Size(987, 650);
			this.dataGridView1.TabIndex = 0;
			this.Timestamp.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.None;
			this.Timestamp.HeaderText = "Timestamp";
			this.Timestamp.MaxInputLength = 25;
			this.Timestamp.Name = "Timestamp";
			this.Timestamp.ReadOnly = true;
			this.Timestamp.Width = 140;
			this.DTC.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.None;
			this.DTC.HeaderText = "DTC";
			this.DTC.MaxInputLength = 4;
			this.DTC.Name = "DTC";
			this.DTC.ReadOnly = true;
			this.DTC.Width = 54;
			this.Status.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.None;
			this.Status.HeaderText = "Status";
			this.Status.MaxInputLength = 8;
			this.Status.Name = "Status";
			this.Status.ReadOnly = true;
			this.Status.Width = 80;
			this.entrycount.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.None;
			this.entrycount.HeaderText = "Event Count";
			this.entrycount.Name = "entrycount";
			this.entrycount.ReadOnly = true;
			this.entrycount.Width = 54;
			this.Description.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
			this.Description.HeaderText = "Description";
			this.Description.MaxInputLength = 300;
			this.Description.Name = "Description";
			this.Description.ReadOnly = true;
			this.toolStripEv.Items.AddRange(new System.Windows.Forms.ToolStripItem[4] { this.toolStripSeparator1, this.toolStripButtonFileInfo, this.toolStripButtonEvShowFilter, this.toolStripLabel1 });
			this.toolStripEv.Location = new System.Drawing.Point(0, 0);
			this.toolStripEv.Name = "toolStripEv";
			this.toolStripEv.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
			this.toolStripEv.Size = new System.Drawing.Size(1016, 25);
			this.toolStripEv.Stretch = true;
			this.toolStripEv.TabIndex = 1;
			this.toolStripEv.Text = "toolStripEv";
			this.toolStripSeparator1.Name = "toolStripSeparator1";
			this.toolStripSeparator1.Size = new System.Drawing.Size(6, 25);
			this.toolStripButtonFileInfo.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
			this.toolStripButtonFileInfo.Image = (System.Drawing.Image)resources.GetObject("toolStripButtonFileInfo.Image");
			this.toolStripButtonFileInfo.ImageTransparentColor = System.Drawing.Color.Magenta;
			this.toolStripButtonFileInfo.Name = "toolStripButtonFileInfo";
			this.toolStripButtonFileInfo.Size = new System.Drawing.Size(23, 22);
			this.toolStripButtonFileInfo.Text = "toolStripButton2";
			this.toolStripButtonFileInfo.ToolTipText = "Shows infos about Log-File";
			this.toolStripButtonFileInfo.Click += new System.EventHandler(b_FileInfoClick);
			this.toolStripButtonEvShowFilter.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
			this.toolStripButtonEvShowFilter.Image = (System.Drawing.Image)resources.GetObject("toolStripButtonEvShowFilter.Image");
			this.toolStripButtonEvShowFilter.ImageTransparentColor = System.Drawing.Color.Magenta;
			this.toolStripButtonEvShowFilter.Name = "toolStripButtonEvShowFilter";
			this.toolStripButtonEvShowFilter.Size = new System.Drawing.Size(23, 22);
			this.toolStripButtonEvShowFilter.Text = "EvShowFilter";
			this.toolStripButtonEvShowFilter.ToolTipText = "Star Event Filter";
			this.toolStripButtonEvShowFilter.Click += new System.EventHandler(b_SetFilter_Click);
			this.toolStripLabel1.Name = "toolStripLabel1";
			this.toolStripLabel1.Size = new System.Drawing.Size(97, 22);
			this.toolStripLabel1.Text = "Show Event Filter";
			this.notvalidtimestampLabel.AutoSize = true;
			this.notvalidtimestampLabel.BackColor = System.Drawing.SystemColors.Info;
			this.notvalidtimestampLabel.Location = new System.Drawing.Point(124, 682);
			this.notvalidtimestampLabel.Name = "notvalidtimestampLabel";
			this.notvalidtimestampLabel.Size = new System.Drawing.Size(110, 13);
			this.notvalidtimestampLabel.TabIndex = 2;
			this.notvalidtimestampLabel.Text = "** Not valid timestamp";
			base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
			base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			base.ClientSize = new System.Drawing.Size(1016, 697);
			base.Controls.Add(this.notvalidtimestampLabel);
			base.Controls.Add(this.toolStripEv);
			base.Controls.Add(this.dataGridView1);
			base.Icon = (System.Drawing.Icon)resources.GetObject("$this.Icon");
			base.Name = "UIEvents";
			this.Text = "Event -Recorder";
			base.SizeChanged += new System.EventHandler(b_EventWinResizeEnd);
			((System.ComponentModel.ISupportInitialize)this.dataGridView1).EndInit();
			this.toolStripEv.ResumeLayout(false);
			this.toolStripEv.PerformLayout();
			base.ResumeLayout(false);
			base.PerformLayout();
		}
	}
}
